package com.bnlr.lrparking.appui.guide;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bnlr.lrparking.appui.home.HomeActivity;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;

/**
 * 引导页
 */
public class Splish_PagerActivity extends BaseActivity {
	private ViewPager viewPager;
	private Button btnNowUse;
	private int[] imgArray = new int[]{R.drawable.ic_guide_1,R.drawable.ic_guide_2,R.drawable.ic_guide_3};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splish__pager);
		viewPager = (ViewPager) this.findViewById(R.id.guide_viewPager);
		btnNowUse = (Button) this.findViewById(R.id.btn_now_use);
		viewPager.setOffscreenPageLimit(2);
		viewPager.setAdapter(new GuidePagerAdapter());
		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}
			@Override
			public void onPageSelected(int position) {
				if (position == 2) {
					btnNowUse.setVisibility(View.VISIBLE);
				} else {
					btnNowUse.setVisibility(View.GONE);
				}
			}
			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		// 设置跳转
		btnNowUse.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AppConfig.getInstance().putString("splish", "1");
				startActivity(HomeActivity.class);
				finish();
			}
		});
	}
	public class GuidePagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return imgArray.length;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView imageView = new ImageView(Splish_PagerActivity.this);
			imageView.setBackgroundResource(imgArray[position]);
			container.addView(imageView);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}
}
